from random import choice
import sys

class Player:
	def __init__(self, marker, playerName):
		#Player's marker on the board
		self.marker = marker
		#The player's name in game
		self.playerName = playerName
		
		#a list of legal moves in the game
		self.legalMoves = range(9)
	
	def messageToPlayer(self, message):
		message = self.playerName + ": " + message
		print message
	
	def move(self, lastMove):
		try:
			#lastMove is None if this is the first move, otherwise we remove it
			if lastMove != None:
				self.legalMoves.remove(lastMove)
		except ValueError:
			#move not found in legal move, handle exception elsewhere
			raise ValueError
		
		#If Test is running a random number is chosen from legalMoves
		f = sys._getframe(1)
		c = f.f_code
		if (c.co_name == "testMoveLegalInput"):
			move = choice(self.legalMoves)
			self.legalMoves.remove(move)
			return move
			
		while 1:
			try:
				move = int(raw_input("Make a move >> "))
			except ValueError:
				print "That wasn't a number, try again!"
				continue
			
			if move in self.legalMoves:
				self.legalMoves.remove(move)
				return move
			else:
				print "Illegal move, try again"
